﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Manage
{
    public class AptechManager
    {
        public int studentCount;
        public int classCount;
        public List<AptechClass> AptechClasses { get; set;}
        public List<AptechStudent> AptechStudents { get; set; }

        public List<AptechClass> searchClass(string search)
        {
            if (search == null || search.Length == 0)
                return AptechClasses;
            List<AptechClass> clazzs = new List<AptechClass>();
            search = search.ToLower();
            foreach (AptechClass clazz in AptechClasses) 
            {
                if ((clazz.Name ?? "").ToLower().Contains(search)
                    || (clazz.Room ?? "").ToLower().Contains(search)
                    || (clazz.Teacher ?? "").ToLower().Contains(search)
                    || (clazz.TimeSlot ?? "").ToLower().Contains(search))
                {
                    clazzs.Add(clazz);
                }
            }
            return clazzs;
        }

        public List<AptechStudent> searchStudent(string search)
        {
            if (search == null || search.Length == 0)
                return AptechStudents;
            List<AptechStudent> students = new List<AptechStudent>();
            search = search.ToLower();
            foreach (AptechStudent stu in AptechStudents)
            {
                if ((stu.Address ?? "").ToLower().Contains(search)
                    || (stu.Name ?? "").ToLower().Contains(search))
                {
                    students.Add(stu);
                }
            }
            return students;
        }

        public void addStudent(AptechStudent stu)
        {
            
            stu.ID = studentCount++;
            if (this.AptechStudents == null)
                this.AptechStudents = new List<AptechStudent>();
            this.AptechStudents.Add(stu);
            
        }

        public void addClass(AptechClass cls)
        {
            cls.ID = classCount++;
            if (this.AptechClasses == null)
                this.AptechClasses = new List<AptechClass>();
            this.AptechClasses.Add(cls);
        }



        public bool updateStudent(AptechStudent stu)
        {
            foreach (AptechStudent i in AptechStudents)
            {
                if (stu.ID == i.ID)
                {
                    i.ClassID = stu.ClassID;
                    i.Name = stu.Name;
                    i.Address = stu.Address;
                    return true;
                }
            }
            return false;

        }

        public bool updateClass(AptechClass cls)
        {
            foreach(AptechClass i in AptechClasses)
            {
                if (cls.ID == i.ID)
                {
                    i.Name = cls.Name;
                    i.Room = cls.Room;
                    i.TimeSlot = cls.TimeSlot;
                    i.Teacher = cls.Teacher;
                    return true;
                }
            }
            return false;
        }

        public bool deleteStudent(int ID)
        {
            foreach(AptechStudent i in AptechStudents)
            {
                if(ID == i.ID)
                {
                    AptechStudents.Remove(i);
                    return true;
                }
            }
            return false;
        }

     public bool deleteClass(int ID)
        {
         foreach(AptechClass i in AptechClasses)
         {
             if(ID == i.ID)
             {
                 AptechClasses.Remove(i);
                 return true;
             }
         }
         return false;
        }



        public static void Save(AptechManager am)
        {
            using (Stream stream = new FileStream("AptechManager.xml", FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AptechManager));
                serializer.Serialize(stream, am);
            }
        }

        public static AptechManager Read()
        {
            try
            {
                using (Stream stream = new FileStream("AptechManager.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(AptechManager));
                    AptechManager c = (AptechManager)serializer.Deserialize(stream);
                    return c;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return new AptechManager();
            }
        }
    }
}
